import requests
import json
import csv


pageSize = 200
pageNumber = 0
session = requests.Session()
header = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,or;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cookie': 'XSRF-TOKEN=c241fb50-f129-412e-845e-21c2e9b5a5b3; SESSION=YTA4MjU3N2UtODk4My00NGE4LTllODctNjc0YzcyZDI3OTY4',
'Host': '10.58.153.201',
'Pragma': 'no-cache',
'Referer': 'http://10.58.153.201/hds-ui/medical/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
'X-XSRF-TOKEN': '3f53efca-3483-43c3-8348-3da1c4c1e05d'
}


def zhenduan (url) :
    params = {
        'pageSize': pageSize,
        'pageNumber': 1,
        'total': 181
    }
    response = requests.get(url= url,params=params,verify=False,headers=header)
    base_data = response.json()
    print(base_data)
    with open('doctor.json',mode='w', newline='', encoding='utf-8') as f :
           json.dump(base_data,f,ensure_ascii=False)
            # with open ('疾病诊断两定.xlsx',mode='a', newline='', encoding='utf-8') as f :
            #         csv_writer = csv.writer(f)
            #         csv_writer.writerow(['诊断名称', '诊断编码'])
            #         key = csv_writer.writerow([key, pv_value.strip()])

def xiangmu (url):
    params = {
        'pageSize': pageSize,
        'pageNumber': 1,
        'total': 181
    }
    response = requests.get(url=url, params=params, verify=False, headers=header)
    base_data = response.json()
    data_list = base_data["data"]["result"]
    all_keys = {k for item in data_list for k in item.keys()}
    with open('项目.csv', mode='w', newline='', encoding='utf-8') as f:
        writer = csv.DictWriter(f, fieldnames=all_keys)
        writer.writeheader()
        writer.writerows(data_list)


def cailiao (url):
    params = {
        'pageSize': pageSize,
        'pageNumber': 1,
        'total': 51308
    }
    response = requests.get(url=url, params=params, verify=False, headers=header)
    base_data = response.json()
    data_list = base_data["data"]["result"]
    all_keys = {k for item in data_list for k in item.keys()}
    with open('材料.csv', mode='w', newline='', encoding='utf-8') as f:
        writer = csv.DictWriter(f, fieldnames=all_keys)
        writer.writeheader()
        writer.writerows(data_list)

if __name__ == '__main__':
    zhenduan_url = f'http://10.58.153.201/hds/web/ldrinfob/lDrInfoB/page?pageSize=200&pageNumber=1&total=181&medinsCode=H32010200872'
    xiangmu_url = f'http://10.58.153.201/hds/web/lcenttrtservdpotb/lCentTrtServDpotB/lCentTrtServDpotBQueryPageunch?pageSize=8383&pageNumber=1&total=0&medListCodg=&fixNo=H32010200872&dpotType=0'
    cailiao_url = f'http://10.58.153.201/hds/web/centmatllistmgt/lCentMatlDpotB/pageUnch?pageSize=51308&pageNumber=1&total=0&dpotType=0&fixNo=H32010200872'
    # xiangmu(xiangmu_url)
    cailiao(cailiao_url)